package com.warmlight.voicepacket.widget;

import android.content.Context;
import android.os.Build;
import android.support.annotation.RequiresApi;
import android.util.AttributeSet;
import android.view.View;
import android.widget.RelativeLayout;
import android.widget.TextView;

import com.warmlight.voicepacket.R;

/**
 * Created by jason on 2018/5/23.
 */

public class CoverLoading extends RelativeLayout {

    private Context mContext;
    private RelativeLayout mContainer;
    private TextView cover_loading, cover_fail, cover_null;


    public CoverLoading(Context context) {
        super(context);
        mContext = context;
        init();
    }

    public CoverLoading(Context context, AttributeSet attrs) {
        super(context, attrs);
        mContext = context;
        init();
    }

    public CoverLoading(Context context, AttributeSet attrs, int defStyleAttr) {
        super(context, attrs, defStyleAttr);
        mContext = context;
        init();
    }

    @RequiresApi(api = Build.VERSION_CODES.LOLLIPOP)
    public CoverLoading(Context context, AttributeSet attrs, int defStyleAttr, int defStyleRes) {
        super(context, attrs, defStyleAttr, defStyleRes);
        mContext = context;
        init();
    }

    private void init(){
        mContainer = (RelativeLayout) View.inflate(mContext, R.layout.cover_loading,this);

        cover_loading = (TextView) mContainer.findViewById(R.id.cover_loading);
        cover_loading.setVisibility(View.VISIBLE);
        cover_null = (TextView) mContainer.findViewById(R.id.cover_null);
        cover_null.setVisibility(View.GONE);
        cover_fail = (TextView) mContainer.findViewById(R.id.cover_fail);
        cover_fail.setVisibility(View.GONE);
        cover_fail.setOnClickListener(new OnClickListener() {
            @Override
            public void onClick(View v) {
                if(mListener != null){
                    mListener.onReloadClicked();
                }
            }
        });
    }

    public enum LoadingResult {
        /**
         * 加载成功
         */
        SUCCESS,
        /**
         * 数据为空
         */
        NULL,

        /**
         * 加载中
         */
        LOADING,

        /**
         * 加载出错
         */
        FAIL
    }

    //设置cover状态
    public void setResult(LoadingResult result){
        switch (result){
            case SUCCESS:
                cover_loading.setVisibility(View.GONE);
                cover_null.setVisibility(View.GONE);
                cover_fail.setVisibility(View.GONE);
                break;
            case LOADING:
                cover_loading.setVisibility(View.VISIBLE);
                cover_null.setVisibility(View.GONE);
                cover_fail.setVisibility(View.GONE);
                break;
            case FAIL:
                cover_loading.setVisibility(View.GONE);
                cover_null.setVisibility(View.GONE);
                cover_fail.setVisibility(View.VISIBLE);
                break;
            case NULL:
                cover_loading.setVisibility(View.GONE);
                cover_null.setVisibility(View.VISIBLE);
                cover_fail.setVisibility(View.GONE);
                break;
            default:
                break;
        }
    }

    //加载失败，重新加载监听
    private ReloadListener mListener;
    public interface ReloadListener{
        void onReloadClicked();
    }
    public void setOnReloadClickListener(ReloadListener listener){
        mListener = listener;
    }

}
